package com.atguigu.petstore.sys.service.impl;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.atguigu.petstore.common.constant.RedisConst;
import com.atguigu.petstore.common.execption.NeedLoginPetStoreException;
import com.atguigu.petstore.common.execption.PetstoreException;
import com.atguigu.petstore.common.result.ResultCodeEnum;
import com.atguigu.petstore.common.util.MD5;
import com.atguigu.petstore.sys.vo.SysUserHeaderImgUpdateVo;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.petstore.sys.entity.SysUser;
import com.atguigu.petstore.sys.service.SysUserService;
import com.atguigu.petstore.sys.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
* @author lfy
* @description 针对表【sys_user(系统-用户)】的数据库操作Service实现
* @createDate 2023-09-21 13:53:29
*/
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
    implements SysUserService{


    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public void updateUser(SysUser sysUser) {

        if(sysUser.getId()==null){
            throw new PetstoreException(ResultCodeEnum.USER_ID_REQUIRED);
        }

        LambdaUpdateChainWrapper<SysUser> update = lambdaUpdate();
        //前端有可能提交 空串，代表依然用自己的旧密码
        if(StringUtils.hasText(sysUser.getLoginPwd())){
            update = update.set(SysUser::getLoginPwd,MD5.encrypt(sysUser.getLoginPwd()));
        }


        LambdaUpdateChainWrapper<SysUser> wrapper = update
                .set(SysUser::getAccountStatus, sysUser.getAccountStatus())
                .set(SysUser::getSafeEmail, sysUser.getSafeEmail())
                .eq(SysUser::getId, sysUser.getId());

        //前端带了头像就改
        if(StringUtils.hasText(sysUser.getHeaderImg())){
            wrapper.set(SysUser::getHeaderImg,sysUser.getHeaderImg());
        }

        wrapper.update();

    }

    @Override
    public void addUser(SysUser user) {

        //检查唯一性
        Long count = lambdaQuery().eq(SysUser::getLoginName, user.getLoginName()).count();
        if(count > 0){
            //数据库已存在
            throw new PetstoreException(ResultCodeEnum.USER_HAS_EXIST);
        }


        //准备数据
        user.setLoginPwd(MD5.encrypt(user.getLoginPwd()));
        user.setHeaderImg("/1.jpg");
        user.setCreateTime(new Date());

        save(user);
    }

    @Override
    public void updateHeaderImg(String token, SysUserHeaderImgUpdateVo vo) {
        //前端传递来的用户id
        Long id = vo.getId();
        String headerImg = vo.getHeaderImg();

        //前置权限校验； 本人才能改本人
        String json = redisTemplate.opsForValue().get(RedisConst.USER_LOGIN_KEY + token);
        if(!StringUtils.hasText(json)){
            //以后强制要求前端去登录页的，请抛出这个异常
            throw new NeedLoginPetStoreException(ResultCodeEnum.USER_INVAILD);
        }

        //转成json
        SysUser user = JSON.parseObject(json, SysUser.class);
        if (!user.getId().equals(id)) {
            throw new NeedLoginPetStoreException(ResultCodeEnum.USER_INVAILD);
        }


        //修改数据
        lambdaUpdate()
                .set(SysUser::getHeaderImg,vo.getHeaderImg())
                .eq(SysUser::getId,vo.getId())
                .update();
    }
}




